package com.key.gradle.test.service.impl;

import java.util.concurrent.Future;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import com.key.gradle.test.service.AsyncTestService;

@Service
public class AsyncTestServiceImpl implements AsyncTestService {

	private final static Logger logger = LoggerFactory.getLogger(AsyncTestServiceImpl.class);

	@Async
	@Override
	public Future<String> runDefaultAsyncExecutor(int taskNo) {
		String threadName = Thread.currentThread().getName();
		logger.info("Execute method asynchronously default - [{}] - {}", taskNo, threadName);
		try {
			Thread.sleep(5000);
			logger.info("Execute method asynchronously default - [{}] done", taskNo);
			return new AsyncResult<String>("Default Async Executor - " + taskNo);
		} catch (InterruptedException e) {
			logger.error("Execute method asynchronously default - [" + taskNo + "] Interrupted Exception", e);
			return AsyncResult.forExecutionException(e);
		}
	}

	@Async("anotherAsyncExecutor")
	@Override
	public void runAnotherAsyncExecutor(int taskNo) {
		String threadName = Thread.currentThread().getName();
		logger.info("Execute method asynchronously another - [{}] - {}", taskNo, threadName);
		try {
			Thread.sleep(5000);
			logger.info("Execute method asynchronously another - [{}] done", taskNo);
		} catch (InterruptedException e) {
			logger.error("Execute method asynchronously another - [" + taskNo + "] Interrupted Exception", e);
		}
	}

}
